package com.bupt.base;

import com.bupt.annotation.MParam;
import com.bupt.constants.InitConstants;
import com.bupt.exception.BaseException;
import com.bupt.utils.ExploreWriteUtils;
import com.bupt.utils.excel.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * excel的操作
 *
 * @author breakpoint/赵先生 <zlgtop@163.com>
 * create on 2021/05/15
 */
public interface ExcelProcedure {

    /*
           eg:
            ExcelField: 是标志这当前的这个属性 的header上面的显示的汉字 如果没有 那么就 显示该属性 名字
            // 注意访问的权限  public 可以访问其他的情况有可能 获取不到数据
            public static class Data {

                @ExcelField(name = "名字")
                private String name;
                @ExcelField(name = "年龄")
                private Integer age;

                public Data(String name, Integer age) {
                    this.name = name;
                    this.age = age;
                }

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public Integer getAge() {
                    return age;
                }

                public void setAge(Integer age) {
                    this.age = age;
                }
            }
     */
    //下载excel 的service
    default <T> Object downLoadExcel(@MParam("请求对象") HttpServletRequest request,
                                     @MParam("响应对象") HttpServletResponse response,
                                     @MParam("下载的数据") List<T> downLoadData,
                                     @MParam("下载的文件的名字") String fileName,
                                     @MParam("sheetName") String sheetName,
                                     @MParam("是否下载模版") boolean isMould) throws BaseException {
        try {
            ExploreWriteUtils.downLoadExcelByCurrentData(request, response, downLoadData, fileName, sheetName, isMould);
            return InitConstants.OK_MESSAGE;
        } catch (IOException e) {
            throw new BaseException("下载失败：" + e.getMessage());
        }
    }

    // 在一个excel 表里面下载多个页面的数据
    default <T> Object downLoadExcel(@MParam("请求对象") HttpServletRequest request,
                                     @MParam("响应对象") HttpServletResponse response,
                                     @MParam("下载的数据") LinkedHashMap<String, List<T>> dataMapVal,
                                     @MParam("下载的文件的名字") String fileName,
                                     @MParam("是否下载模版") boolean isMould) throws BaseException {
        try {
            ExploreWriteUtils.downLoadExcelByCurrentData(request, response, dataMapVal, fileName, isMould);
            return InitConstants.OK_MESSAGE;
        } catch (IOException e) {
            throw new BaseException("下载失败：" + e.getMessage());
        }
    }

    //下载excel 的service
    default <T> Object downLoadExcel(@MParam("请求对象") HttpServletRequest request,
                                     @MParam("响应对象") HttpServletResponse response,
                                     @MParam("下载的数据") List<T> downLoadData,
                                     @MParam("下载的文件的名字") String fileName,
                                     @MParam("sheetName") String sheetName) throws BaseException {
        try {
            ExploreWriteUtils.downLoadExcelByCurrentData(request, response, downLoadData, fileName, sheetName, false);
            return InitConstants.OK_MESSAGE;
        } catch (IOException e) {
            throw new BaseException("下载失败：" + e.getMessage());
        }
    }

    // 在一个excel 表里面下载多个页面的数据
    default <T> Object downLoadExcel(@MParam("请求对象") HttpServletRequest request,
                                     @MParam("响应对象") HttpServletResponse response,
                                     @MParam("下载的数据") Map<String, List<T>> dataMapVal,
                                     @MParam("下载的文件的名字") String fileName) throws BaseException {
        try {
            ExploreWriteUtils.downLoadExcelByCurrentData(request, response, dataMapVal, fileName, false);
            return InitConstants.OK_MESSAGE;
        } catch (IOException e) {
            throw new BaseException("下载失败：" + e.getMessage());
        }
    }

    // 较为详细的约束上传文件的类型
    default <T> List<T> parseExcelDataFromExcelInputStream(MultipartFile file, Class<T> clazz)
            throws BaseException {
        String originalFilename = file.getOriginalFilename();
        if (null != originalFilename && (originalFilename.endsWith(".xls") || originalFilename.endsWith(".xlsx"))) {
            try {
                // 解析上传的文件信息
                return ExcelUtils.getDataList(file.getInputStream(), clazz);
            } catch (IOException e) {
                throw new BaseException("获取上传文件失败 e=" + e.getMessage());
            }
        } else {
            throw new BaseException("上传文件的格式错误，请选择 excel 进行上传");
        }
    }

    // 解析多页的excel的操作
    default <T> Map<String, List<T>> parseExcelDataMapFromExcelInputStream(
            @MParam("文件信息") MultipartFile file,
            @MParam("类信息") Map<String, Class<? extends T>> clazzMap) throws BaseException {
        String originalFilename = file.getOriginalFilename();
        if (null != originalFilename && (originalFilename.endsWith(".xls") || originalFilename.endsWith(".xlsx"))) {
            try {
                // 解析上传的文件信息
                return ExcelUtils.getDataMapFromCurrentInput(file.getInputStream(), clazzMap);
            } catch (IOException e) {
                throw new BaseException("获取上传文件失败 e=" + e.getMessage());
            }
        } else {
            throw new BaseException("上传文件的格式错误，请选择 excel 进行上传");
        }
    }
}
